package com.ruoyi.web.controller.stage;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.StuCourse;
import com.ruoyi.course.mapper.StuCourseMapper;
import com.ruoyi.course.service.StuCourseService;
import com.ruoyi.major.domain.StuMagor;
import  com.ruoyi.stage.domain.StuStage;
import com.ruoyi.stage.service.IStuStageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/stage/stagelist")
public class StuStageController extends BaseController {
    @Resource
    private IStuStageService stuStageService;

    @Resource
    private StuCourseService stuCourseService;

    @PreAuthorize("@ss.hasPermi('stage:stagelist:list')")
    @GetMapping("/list")
    public TableDataInfo list(StuStage stuStage)
    {
        startPage();
        List<StuStage> list = stuStageService.selectStage(stuStage);
        return getDataTable(list);
    }

    /**查询阶段详细信息，实现动态加载
     *
     *
     */
    @PreAuthorize("@ss.hasPermi('stage:stagelist:query')")
    @GetMapping(value = { "/", "/{stageId}" })
    public AjaxResult getInfo(@PathVariable(value = "stageId", required = false) Long stageId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("courses", stuCourseService.selectAllCourses());
        if (StringUtils.isNotNull(stageId))
        {
            StuStage stuStage = stuStageService.selectStageById(stageId);
            ajax.put(AjaxResult.DATA_TAG, stuStage);
            ajax.put("courIds", stuCourseService.selectStuCoursesListByStageId(stageId));
        }
        return ajax;
    }

    /**添加阶段
     *
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('stage:stagelist:add')")
    public AjaxResult add(@Validated @RequestBody StuStage stuStage)
    {
        if (UserConstants.NOT_UNIQUE.equals(stuStageService.checkStageNameUnique(stuStage)))
        {
            return AjaxResult.error("新增阶段'" + stuStage.getStageName() + "'失败，阶段名称已存在");
        }
        stuStage.setCreateBy(getUsername());
        return toAjax(stuStageService.insertStuStage(stuStage));
    }

    /**修改阶段
     *
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('stage:stagelist:edit')")
    public AjaxResult edit(@Validated @RequestBody StuStage stuStage)
    {
        if (UserConstants.NOT_UNIQUE.equals(stuStageService.checkStageNameUnique(stuStage)))
        {
            return AjaxResult.error("修改阶段'" + stuStage.getStageName() + "'失败，阶段名称已存在");
        }
        stuStage.setUpdateBy(getUsername());
        return toAjax(stuStageService.updateStuStage(stuStage));
    }

    /**
     * 删除阶段
     */
    @DeleteMapping("/{stageIds}")
    @PreAuthorize("@ss.hasPermi('stage:stagelist:remove')")
    public AjaxResult remove(@PathVariable Long[] stageIds)
    {
        return toAjax(stuStageService.deleteStuStageByIds(stageIds));
    }


    @Log(title = "阶段管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StuStage stuStage)
    {
        List<StuStage> list = stuStageService.selectStage(stuStage);
        ExcelUtil<StuStage> util = new ExcelUtil<StuStage>(StuStage.class);
        util.exportExcel(response, list, "阶段数据");
    }


}
